﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using IronTrishul.Web;
using OpenCVQuote;
using System.Xml.Linq;

namespace dontnet_vbfamily
{
    /// <summary>
    /// Summary description for DotNetWebCService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class DotNetWebCService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string PlaceOrder(string advertizementString, string senderWsdl)
        {
            dynamic proxy = new Http(new Uri(senderWsdl));
            //skillset...
            OpenResume quote = new OpenResume();
            dynamic ad = ParseInput(advertizementString);

            dotnet_cfamily.DataLayer.ResumeDatabaseEntities db = new dotnet_cfamily.DataLayer.ResumeDatabaseEntities(new Uri(@"http://localhost:1738/Service/ResumeDataService.svc/"));

            OpenResume.advertizementDataTable adt = new OpenResume.advertizementDataTable();
            //generate this skill set...
            for (int i = 0; i < ad.advertizement.Count; i++)
            {
                dynamic d = ad.advertizement[i];
                OpenCVQuote.OpenResume.advertizementRow ads = adt.NewadvertizementRow();
                ads.skillfamily = d.skillfamily;
                ads.skillid = d.skillfamily;
                ads.skillname = d.skillname;
                ads.totalmonths = d.totalmonths;
                adt.AddadvertizementRow(ads);
            }

            //

            var advertizementskills = from adsk in adt
                                      select adsk;
            //var advertizementskills = from adsk in quote.advertizement
            //                          select adsk;

            /*
            var matchedcandidates = from madsk in db.candidate_skills
                                    join dbskil in advertizementskills
                                    on madsk.skillid equals dbskil.skillid
                                    select madsk;

            var foundCandidate = from matchcandidate in db.candidates
                                 join skill in matchedcandidates
                                     on matchcandidate.candidateid equals skill.candidateid
                                 select matchcandidate;

            foreach (var cand in foundCandidate.ToList())
            {
                quote.candidate.AddcandidateRow(cand.username, cand.firstname, cand.lastname, cand.address, cand.email, cand.mobile, cand.consultantid.Value);
                var skillset = from adsk in db.candidate_skills
                               where adsk.candidateid == cand.candidateid
                               select adsk;
                foreach (var skillq in skillset.ToArray())
                {
                    OpenCVQuote.OpenResume.candidate_skillRow skills = quote.candidate_skill.Newcandidate_skillRow();
                    skills.candidateid = cand.candidateid;
                    skills.skillfamily = skillq.skillmaster.skillfamily;
                    skills.skillid = skillq.skillmaster.skillid;
                    skills.skillname = skillq.skillmaster.skillname;
                    quote.candidate_skill.Addcandidate_skillRow(skills);
                }
            }
            */
            object result = proxy.SendQuote(quote);
            return result.ToString();
        }
        private object ParseInput(string xmlinstring)
        {

            dynamic viewData = XElement.Parse(xmlinstring);//.ToElastic();
            return viewData;
        }
    }
}
